const app=new Vue({
    el:'#app',
    data: {
        books: [
          {
            id: 1,
            name: "《被讨厌的勇气》",
            date: "2006-2",
            price: 85.00,
            count: 1
          },
          {
            id: 2,
            name: "《人性的光辉》",
            date: "2008-2",
            price: 49.00,
            count: 1
          },
          {
            id: 3,
            name: "《活着》",
            date: "2006-2",
            price: 34.00,
            count: 1
          },
          {
            id: 4,
            name: "《当下的力量》",
            date: "2007-2",
            price: 42.00,
            count: 1
          },
          {
            id: 5,
            name: "《国富论》",
            date: "2001-2",
            price: 58.00,
            count: 1
          }
        ]
      },
      methods: {
        decrement(index){
            this.books[index].count--
        },
        increment(index){
            this.books[index].count++
        },
        removeBook(index){
            this.books.splice(index,1)
        }
      },
      computed:{
        totalPrice(){
          /*   let totalPrice=0
            for(let i=0;i<this.books.length;i++){
                totalPrice+=this.books[i].price*this.books[i].count
            }
            return totalPrice */
            return this.books.reduce(function(preValue,book){
                return preValue+book.price*book.count
            },0)
        }
      },
    //   使用过滤器处理价格保留两位和人民币符号
    filters:{
        finalPrice(price){
            return '￥'+price.toFixed(2)
        }
    }
})